const wx = require('./common');
const db=uniCloud.database()
// 添加素材
async function add(data) {
	// 获取参数
	
	// 添加到后台服务器
	let addRes=await db.collection('file').add(data)
	return addRes
}
// 修改素材
async function edit(data) {

}
// 获取单个素材
async function get(data) {

}
// 删除素材
async function deleted(data) {

}
async function getFileList(data){
	let count=data.count||20
	let offset=data.offset||0
	let res=await db.collection('file').limit(count).skip(count*offset).get()
	return res
}
// 获取素材列表
async function getList(data) {
	let access_token = await wx.getToken()
	let form = {
		type: "image",
		offset: data.offset||0,
		count: data.count||20
	}
	let listUrl =
		`https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=${access_token}`
	let listRes = await uniCloud.httpclient.request(listUrl, {
		method: 'POST',
		data: JSON.stringify(form),
		dataType: 'json'
	})
	if(listRes.status===200){
		return {
			data: listRes.data,
			status: 200
		}
	}else {
		return {
			msg: '服务器出错',
			data: false,
			status: 400
		}
	}
}
// 获取素材数量
async function getCount(data) {

}


module.exports = {
	add,
	edit,
	get,
	deleted,
	getList,
	getCount,
	getFileList
}
